package com.fauks.ssm.mybatis;

import com.fauks.ssm.mybatis.bean.MNews;
import com.fauks.ssm.mybatis.mapper.SelfAssocaMapper;
import com.fauks.ssm.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * 自关联查询
 */
public class _05SelfAssocia {

    private SqlSession sqlSession;
    private SelfAssocaMapper selfAssociaMapper;

    @Before
    public void initDao() {
        sqlSession = MyBatisUtils.getSqlSession();
        selfAssociaMapper = sqlSession.getMapper(SelfAssocaMapper.class);
    }

    @After
    public void closeSession() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    @Test
    public void testGetChildByParent() {
        List<MNews> childByParent = selfAssociaMapper.selectChildrenByParent(2);
        for (MNews mNews : childByParent) {
            System.out.println(mNews);
        }
    }

    @Test
    public void testSelectById() {
        MNews mNews = selfAssociaMapper.selectById(1);
        System.out.println(mNews);
    }

    @Test
    public void testManyToOne() {
        MNews mNews = selfAssociaMapper.selectNewsAndParent(7);
        System.out.println(mNews);
    }
}
